package com.gs.tests.seleniumextjs;

import java.io.FileInputStream;

import jxl.Sheet;
import jxl.Workbook;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.safari.SafariDriver;

public class Utils {
	
	public static WebDriver getDriverInstance(String browser){
		WebDriver d;
		
		switch (browser.toLowerCase()) {			
			case "firefox":				{d = new FirefoxDriver(); break;}
			case "chrome":				{d = new ChromeDriver(); break;}
			case "internet explorer":	{d = new InternetExplorerDriver(); break;}
			case "safari":				{d = new SafariDriver(); break;}
			default: 					{d = new FirefoxDriver();}
		}
		
		return d;
	}
	
	public static void refreshPage(WebDriver driver) throws InterruptedException{
		   Actions actionObject = new Actions(driver);
		   actionObject.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
		   Thread.sleep(3000);
	}
	
	public static void maximizeWindow(WebDriver driver) {
		driver.manage().window().maximize();
	}
	
	public static String[][] getTestData(String xlFile) throws Exception {
	       String[][] arrData = null;

	       // open .xls file
	       FileInputStream f = new FileInputStream(xlFile);
	       Workbook wb = Workbook.getWorkbook(f);	       
	       Sheet sheet = wb.getSheet(0);
	       
	       int rowCount = sheet.getRows();
	       int colCount = sheet.getColumns();
	       
	       // re-allocate array of test data
	       arrData = new String[rowCount-1][colCount];
	       
	       // get test data
	       for (int row = 0; row < rowCount-1; row++)
	          for (int col = 0; col < colCount; col++) 
	    	      arrData[row][col] = sheet.getCell(col, row + 1).getContents().toString();

	       // close .xls file
	       f.close();
	       
	       return arrData;
	   }
	
}
